<template>
  <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="用户名"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="密码"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit" >登陆</van-button>
    <br><br>
    <van-button round block type="primary" to="regist" native-type="submit" style="background:#e54847;border:none">免费注册</van-button>
  </div>
</van-form>
</template>

<script>
import { createApp } from 'vue';
import { Dialog } from 'vant';

export default {
  name: "Login",
  data() {
    return { 
		username: '',
    password: '', 
		isShow: true, 
    lastUrl:''};
  },components: {
    [Dialog.Component.name]: Dialog.Component,
  },
  created(){
    this.lastUrl = window.location.href.split('/')[window.location.href.split('/').length-1];
    console.log(this.lastUrl);
    
  },
  methods: {
	 async onSubmit(values) {
     
      // console.log('submit', values);
	    // console.log(this.username,this.password);
      
      let data = await this.$axios.post('http://phpclub.org.cn:8080/api/user/login',{mobile:this.username,password:this.password});
      console.log(data);
      if(data.code == 0){
        //更改vuex里的信息
        this.$store.commit('updateInfo',data.data);
        //!存储到本地存储
        localStorage.setItem('userName',this.$store.state.userInfo.nickname);
        
        //弹窗
        Dialog.alert({
            title: '登陆成功',
            message: data.msg,
            theme: 'round-button',
          }).then(() => {
            // on close
            //路由到个人信息页面
              this.$router.push('userInfo');
          });
      }else{
        //弹窗
        Dialog.alert({
            title: '错误!!',
            message: data.msg,
            theme: 'round-button',
          })
      }
      return false;
    }
  },
};
</script>

<style scoped>
button{
  background-color:#e54847;
  border: none;
  font-size:16px;
  
}
</style>